package com.keosimage.action;

import org.apache.log4j.Logger;

import weibo4j.Oauth;
import weibo4j.http.AccessToken;

import com.keosimage.common.KeosImageBaseAction;
import com.keosimage.common.KeosImageConstants;
import com.keosimage.common.KeosImageException;
import com.keosimage.common.KeosImageUtils;

public class OauthAction extends KeosImageBaseAction {
	static Logger log = Logger.getLogger(OauthAction.class.getName());
	private static final long serialVersionUID = -3040854883836850635L;

	public String check() throws Exception {
		return SUCCESS;
	}

	public String callback() throws Exception {
		if (KeosImageUtils.isEmpty(this.code)) {
			throw new KeosImageException("Sina Oauth認證失敗！");
		}

		Oauth oauth = new Oauth();
		AccessToken accessToken = oauth.getAccessTokenByCode(this.code);
		log.debug(accessToken);
		
		this.session.setAttribute(KeosImageConstants.ACCESS_TOKEN, accessToken);
		return SUCCESS;
	}

	private String code;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
}
